/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * constructor
 * @param form the form object
 * @param selectName the select name
 * @param multiple
 * @param options the HTML Option objects
 */ 
function HTMLSelector(selectObj,options,equalStrict){
	this.jsjava_class="jsorg.eob.component.selector.HTMLSelector";
	this.equalStrict=equalStrict;
	if(equalStrict==undefined){
		this.equalStrict = true;
	}
	if(options==undefined||!(options instanceof Array)){
		options=new Array(0);
	}
	if(selectObj){
		this.selectObject=selectObj;
		var sL=selectObj.options.length;
		for(var i=0;i<options.length;i++){
			this.selectObj.options[sL+i]=options[i];
		}
	}	
	this.addingListeners=new Array();
}

/**
 * add a option
 * @param option
 * @param overwrite
 */ 
HTMLSelector.prototype.addOption=function(option,overwrite){
	if(overwrite==undefined){
		overwrite=true;
	}
	if(this.selectObject){
		var sObj=this.selectObject;
		if(!this.contains(option)){
		    sObj.options[sObj.options.length]=option;			
		}else if(overwrite){
		    var opt=this.getOptionByValue(option.value);
			opt=option;
		}else{
			
		}
	}
	var s=this.addingListeners.length;
	for(var i=0;i<s;i++){
		eval("this.addingListeners["+i+"](option)");
	}
};

/**
 * add a option item
 * @param value
 * @param text
 * @param overwrite
 */ 
HTMLSelector.prototype.addOptionItem=function(value,text,overwrite){
	var option = new Option(text,value);
	this.addOption(option,overwrite);
};

/**
 * add options
 * @param options
 * @param overwrite
 */ 
HTMLSelector.prototype.addOptions=function(options,overwrite){
	if(this.selectObject){
		if(options==undefined||!(options instanceof Array)){
			options=new Array(0);
		}
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<options.length;i++){
			this.addOption(options[i],overwrite);
		}
	}
};

/**
 * insert a option
 * @param index
 * @param option
 */ 
HTMLSelector.prototype.insertOption=function(index,option){
	if(this.selectObject){
		var sObj=this.selectObject;
		for(var i=sObj.options.length;i>index;i--){
			var temp=sObj.options[i-1];
			sObj.options[i-1]=new Option();
			sObj.options[i]=temp;
		}
		sObj.options[index]=option;
	}
};

/**
 * insert options
 * @param index
 * @param options
 */ 
HTMLSelector.prototype.insertOptions=function(index,options){
	if(options==undefined||!(options instanceof Array)){
		options=new Array(0);
	}
	for(var i=0;i<options.length;i++){
		var option=options[i];
		insertOption.insertOption(index+i,option);
	}
};

/**
 * remove a option
 * @param index
 */ 
HTMLSelector.prototype.removeOption=function(index){
	if(this.selectObject){
		var sObj=this.selectObject;
		sObj.options[index]=null;
	}
};

/**
 * remove all options
 * @param index
 */ 
HTMLSelector.prototype.removeAll=function(){
	var sObj=this.selectObject;
	var sL=sObj.options.length;
	for(var i=0;i<sL;i++){
		sObj.options[i]=null;
	}
};

/**
 * remove options
 * @param fromIndex
 * @param toIndex
 */ 
HTMLSelector.prototype.removeOptionBetween=function(fromIndex,toIndex){
	for(var i=fromIndex;i<toIndex;i++){
		this.removeOption(i);
	}
};

/**
 * Judge whether the option is contained
 * @param option
 */ 
HTMLSelector.prototype.contains=function(option){
	if(option==undefined){
		return false;
	}
	if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<sL;i++){
			var opt=sObj.options[i];
			if(this.equalStrict){
				if(opt.value==option.value&&opt.text==option.text){
					return true;
				}
			}else{
				if(opt.value==option.value){
					return true;
				}
			}
		}
	}
	return false;
};

/**
 * Judge whether the option is contained
 * @param option
 */ 
HTMLSelector.prototype.isEmpty=function(){
	if(!this.selectObject||this.selectObject.options.length==0){
		return true;
	}
	return false;
};

/**
 * get option by a the value property
 */ 
HTMLSelector.prototype.getOptionByValue=function(value){
    if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<sL;i++){
			var option=sObj.options[i];
			if(option.value==value){
				return option;
			}
		}
	}
};

/**
 * get selected options
 */ 
HTMLSelector.prototype.getSelectedOptions=function(){
    var options = new Array();
	if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<sL;i++){
			var option=sObj.options[i];
			if(option.selected){
				options[options.length]=option;
			}
		}
	}
	return options;
};

/**
 * add selected options from a selector to another selector
 * @param toSelector
 */ 
HTMLSelector.prototype.addSelectedOptionsTo=function(toSelector,overwrite){
	var options=HTMLSelectorUtils.cloneOptions(this.getSelectedOptions());	
	if(toSelector&&toSelector.selectObject){
		toSelector.addOptions(options,overwrite);
	}
};

/**
 * add selected options from a selector to another selector
 * @param toSelector
 */ 
HTMLSelector.prototype.addAllOptionsTo=function(toSelector,overwrite){
	var options=HTMLSelectorUtils.cloneOptions(this.selectObject.options);
	if(toSelector&&toSelector.selectObject){
		toSelector.addOptions(options,overwrite);
	}
};

/**
 * remove selected options
 */ 
HTMLSelector.prototype.removeSelectedOptions=function(){
	if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<sL;i++){
			var option=sObj.options[i];
			if(option.selected){
				sObj.options[i]=null;
				sL--;
				i--;
			}
		}
	}
};

/**
 * move selected options upward
 */ 
HTMLSelector.prototype.moveUp=function(){
	if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=0;i<sL;i++){
			var option=sObj.options[i];			
			if(option.selected){
				if(i!=0){
					var preOption=sObj.options[i-1];
					if(!preOption.selected){
						option.swapNode(preOption);
					}
				}
			}
		}
		this.selectObject.focus();
	}
	
};

/**
 * move selected options downward
 */ 
HTMLSelector.prototype.moveDown=function(){
	if(this.selectObject){
		var sObj=this.selectObject;
		var sL=sObj.options.length;
		for(var i=sL-1;i>=0;i--){
			var option=sObj.options[i];			
			if(option.selected){
				if(i!=sL-1){
					var nextOption=sObj.options[i+1];
					if(!nextOption.selected){
						nextOption.swapNode(option);
					}
				}
			}
		}
		this.selectObject.focus();
	}
};
/**
 * add a listener on adding a option to the selector
 * @param func
 */ 
HTMLSelector.prototype.addSelectorAddingListener=function(func){
	this.addingListeners[this.addingListeners.length]=func;
};